var DBFactory = require('../db/mysql_db');
var indexDao = module.exports;

function getGooList(data, cb) {
    var sql = 'select * from shop_goods where is_publish = ? ';
    var arr = [];
    arr.push(1);
    if(data.cid) {
        sql += ' and fq_cat = ? ';
        arr.push(data.cid);
    }
    if(data.key_word) {
        sql += ' and title like ? ';
        arr.push('%'+data.key_word+'%');
    }
    if(parseInt(data.sort_scoreh2l) == 1) {
        sql += ' order by score desc ';
    } else if(parseInt(data.high_volume) == 1) {
        sql += ' order by volume desc ';
    } else if(parseInt(data.sort_scorel2h) == 1){
        sql += ' order by score asc ';
    } else {
        sql += ' order by id desc ';
    }
    sql += ' limit ? , ? ';
    arr.push(data.start);
    arr.push(data.size);
    DBFactory.poolQuery(sql, arr,
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('getGooList Err: ' + err);
                return;
            }
            var index = {};
            index.goodsList = result;
            index.total = data.total;
            index.cid = data.cid;
            getCategory(data, index, cb);
        }
    );
}

function getTotal(data, cb) {
    var sql = 'select count(*) as count from shop_goods where is_publish = ? ';
    var arr = [];
    arr.push(1);
    if(data.cid) {
        sql += ' and fq_cat = ? ';
        arr.push(data.cid);
    }
    if(data.key_word) {
        sql += ' and title like ? ';
        arr.push('%'+data.key_word+'%');
    }

    DBFactory.poolQuery(sql, arr,
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('getTotal Err: ' + err);
                return;
            }
            cb(null, result[0].count);
        }
    );
}

function getCategory(data, index, cb) {
    var sql = 'select * from shop_category ';
    DBFactory.poolQuery(sql, [],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('getCategory Err: ' + err);
                return;
            }
            index.category = result;
            getBanner(data, index, cb);
        }
    );
}

function getBanner(data, index, cb) {
    var sql = 'select * from  shop_banner order by id desc limit 3 ';
    DBFactory.poolQuery(sql, [],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('show Err: ' + err);
                return;
            }
            index.banner = result;
            if(data.uid) {
                getCartCount(data, index, cb )
            } else {
                cb(null, index);
            }
        }
    );
}

function getCartCount(data, index, cb ) {
    var sql = 'select sum(num) as count from shop_cart where uid = ?';
    DBFactory.poolQuery(sql, [data.uid],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('getCartCount Err: ' + err);
                return;
            }
            index.cartCount = result[0].count;
            cb(null, index);
        }
    );
}

indexDao.show = function(data, cb) {
    getTotal(data, function(err, total){
       if(err) {
           cb('获取商品个数失败！');
       } else {
           data.total = total;
           getGooList(data, cb);
       }
    });
}
